/**
 * 
 * CONTROLLER of check box...
 * 
 * 2009-11-21
 * 
 * */
package com.components.form
{
	import core.section.Controller;
	
	import flash.display.DisplayObject;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Event(name="change",type="flash.events.Event")]
	
	public class CheckBox extends Controller
	{
		//START...
		
		//static varriables...
		public static const CHECKED:String = "checked";
		
		public static const UNCHECKED:String = "unchecked";
		
		//PROPERTY...
		
		private var _state:String;
				
		
		//CONSTRUCTOR...
		public function CheckBox(target:DisplayObject)
		{
			super(target);
			
			init();
		}
		
		//METHODS...
		
		// initializing...
		private function init():void
		{
						
			var target:MovieClip = this.target as MovieClip;
									
			target.gotoAndStop(1);
			
			target.alpha = 1;
				
			var labels:Array = target.currentLabels;
								
			labels.sortOn("frame",Array.NUMERIC);	
						
			_state = (labels[0] as FrameLabel).name;
			
			target.addFrameScript((labels[0] as FrameLabel).frame - 1,frm1Script);
			
			target.addFrameScript((labels[1] as FrameLabel).frame - 1,frm2Script);
			
			configEventListeners();
			
		}
		
		//config event listeners...
		private function configEventListeners():void
		{
			
			target.addEventListener(MouseEvent.CLICK,clickHandler);
			
		}
		
		// on target clicked...
		private function clickHandler(evt:MouseEvent):void
		{
			
			state = state == CheckBox.CHECKED ? CheckBox.UNCHECKED : CheckBox.CHECKED;
			
		}
		
		// frame1 script...
		private function frm1Script():void
		{
			
			var target:MovieClip = this.target as MovieClip;
			
			target.stop();

			dispatchEvent( new Event(Event.CHANGE));				
			
		}
		
		
		//frame 2 script...
		private function frm2Script():void
		{
			
			var target:MovieClip = this.target as MovieClip;
			
			target.stop();
									
			dispatchEvent( new Event(Event.CHANGE));
			
		}
		
		// set stage...
		public function set state(val:String):void
		{
		
			if(val != _state)
			{
				
				_state = val;
				
				var target:MovieClip = this.target as MovieClip;
			
				target.play();	
				
			}
						
		}
		
		//get state... 
		public function get state():String
		{
			
			return _state;
						
		}
		
		
		//END...
	}
}